package com.house.controller;

import com.house.common.R;
import com.house.entity.Area;
import com.house.service.AreaService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/area")
public class AreaController {

    @Autowired
    private AreaService areaService;

    /**
     * 保存区域
     * @param area
     * @return
     */
    @PostMapping("/save")
    public R<String> save(@RequestBody Area area) {
        areaService.save(area);
        return R.success("保存成功");
    }
    /**
     * 更新区域
     * @param area
     * @return
     */
    @PutMapping("/update")
    public R<String> update(@RequestBody Area area) {
        areaService.update(area);
        return R.success("更新成功");
    }

    /**
     * 获取区域
     * @return
     */
    @GetMapping("/getAreas")
    public R<List<Area>> getAreas() {
        List<Area> areas = areaService.getAreas();
        return R.success(areas);
    }

    /**
     * 删除区域
     * @param areaId
     * @return
     */
    @DeleteMapping("/delete/{areaId}")
    public R<String> delete(@PathVariable int areaId) {
        int res = areaService.delete(areaId);
        if (res == 0) {
            return R.error("删除失败!");
        }
        return R.success("删除成功!");
    }
}
